Editor can be used to edit an individual item in a database table, without needing a DataTable. Furthermore, it can display the form directly in any element on the page without needing a model display. Both of these abilities are shown in this example.
This type of editing is particularly useful if you wish to show someone a form where they can edit a row that is specific to them - e.g. customer account information for a web-site. You would want to allow them to update their name, contact details, etc, having the form directly on the page and easily accessible, while at the same time making use of Editor's easy setup and form creation.
It is important to note that if you to take this approach, make sure that you have a WHERE statement at the server-side to lock the submission to just the
client you want (e.g. based on a session variable, or similar)!
Another point to make is that if the source row has been deleted, there will be nothing to show and Editor will throw an error. As the data for this form is being reused from other table based forms, it is possible that the row might already have been deleted! If that is the case, please see the Ajax popup editing example to see Editor's standalone Ajax mode in action.
The JavaScript shown below is used to initialise the table shown in this example:
const editor = new DataTable.Editor({
ajax: '/api/staff',
dataSrc: 'ajax',
display: '#myForm',
fields: [
{
label: 'First name:',
name: 'first_name'
},
{
label: 'Last name:',
name: 'last_name'
},
{
label: 'Position:',
name: 'position'
},
{
label: 'Office:',
name: 'office'
},
{
label: 'Extension:',
name: 'extn'
},
{
label: 'Start date:',
name: 'start_date',
type: 'datetime'
},
{
label: 'Salary:',
name: 'salary'
}
],
});
editor
.buttons('Save')
.edit('row_1')
.title('Edit');
In addition to the above code, the following JavaScript library files are loaded for use in this example:
The HTML shown below is the raw HTML table element, before it has been enhanced by DataTables:
This example uses a little bit of additional CSS beyond what is loaded from the library files (below), in order to correctly display the table. The additional CSS used is shown below:
The following CSS library files are loaded for use in this example to provide the styling of the table:
This table loads data by Ajax. The latest data that has been loaded is shown below. This data will update automatically as any additional data is loaded.
The script used to perform the server-side processing for this table is shown below. Please note that this is just an example script using PHP. Server-side processing scripts can be written in any language, using the protocol described in the DataTables documentation.